import { _decorator, Component, Node, AnimationClip, Animation } from 'cc'
import { initParamsNumber, initParamsTrigger, StateMachine } from '../../Base/StateMachine'
import {
  DIRECTION_ENUM,
  ENTITY_STATE_ENUM,
  ENTITY_TYPE_ENUM,
  FSM_PARAMS_TYPE_ENUM,
  PARAMS_NAME_ENUM,
  SPIKES_TYPE_MAP_TOTAL_COUNT_ENUM,
} from '../../Enum'
import { IdleSubStateManagerIron } from '../IronSkeleton/IdleSubStateManagerIron'
import { DeathSubStateMachineIron } from '../IronSkeleton/DeathSubStateMachineIron'
import { DeathSubStateMachineSmoke } from './DeathSubStateMachineSmoke'
import { IdleSubStateMachineSmoke } from './IdleSubStateMachineSmoke'
import { EntityManager } from '../../Base/EntityManager'
const { ccclass, property } = _decorator

/**
 * Predefined variables
 * Name = SmokeStateMachine
 * DateTime = Sat Jul 30 2022 14:48:21 GMT+0800 (中国标准时间)
 * Author = Zero_Y
 * FileBasename = SmokeStateMachine.ts
 * FileBasenameNoExtension = SmokeStateMachine
 * URL = db://assets/Scripts/Smoke/SmokeStateMachine.ts
 * ManualUrl = https://docs.cocos.com/creator/3.4/manual/zh/
 *
 */

@ccclass('SmokeStateMachine')
export class SmokeStateMachine extends StateMachine {
  resetTrigger() {
    for (const [_, value] of this.params) {
      if (value.type === FSM_PARAMS_TYPE_ENUM.TRIGGER) {
        value.value = false
      }
    }
  }

  // 初始化参数
  initParams() {
    this.params.set(PARAMS_NAME_ENUM.IDLE, initParamsTrigger())
    this.params.set(PARAMS_NAME_ENUM.DIRECTION, initParamsNumber())
    this.params.set(PARAMS_NAME_ENUM.DEATH, initParamsTrigger())
  }

  // 初始化状态机
  initStateMachine() {
    this.stateMachines.set(PARAMS_NAME_ENUM.IDLE, new IdleSubStateMachineSmoke(this))
    this.stateMachines.set(PARAMS_NAME_ENUM.DEATH, new DeathSubStateMachineSmoke(this))
  }
  initAnimationEvent() {
    this.animationComponent.on(Animation.EventType.FINISHED, () => {
      // 执行完动画需要恢复默认idle动画的白名单
      const whiteList = ['idle']
      const name = this.animationComponent.defaultClip.name
      if (whiteList.some(v => name.includes(v))) {
        // 统一state入口
        this.node.getComponent(EntityManager).state = ENTITY_STATE_ENUM.DEATH
        // this.currentState = this.stateMachines.get(PARAMS_NAME_ENUM.IDLE)
      }
    })
  }
  async init() {
    // 添加动画组件
    this.animationComponent = this.addComponent(Animation)
    this.initParams()
    this.initStateMachine()
    this.initAnimationEvent()
    // 确保资源资源加载
    await Promise.all(this.waitingList)
  }
  run() {
    switch (this.currentState) {
      case this.stateMachines.get(PARAMS_NAME_ENUM.IDLE):
      case this.stateMachines.get(PARAMS_NAME_ENUM.DEATH):
        if (this.params.get(PARAMS_NAME_ENUM.DEATH).value) {
          this.currentState = this.stateMachines.get(PARAMS_NAME_ENUM.DEATH)
        } else if (this.params.get(PARAMS_NAME_ENUM.IDLE).value) {
          this.currentState = this.stateMachines.get(PARAMS_NAME_ENUM.IDLE)
        } else {
          // 为了触发子状态机的改变
          this.currentState = this.currentState
        }
        break
      default:
        this.currentState = this.stateMachines.get(PARAMS_NAME_ENUM.IDLE)
    }
  }
}
